﻿using System;
using System.Runtime.Serialization;
using System.ComponentModel;
using Navigant.Common;

namespace Navigant.DataContracts
{
    [DataContract(Name = "CaseContract", Namespace = ServiceConstants.DataContractNamespace)]
    public class CaseContract : INotifyPropertyChanged
    {
        #region Fields

        private Guid id;        
        private Guid clientPortalId;        
        private string name;
        private string url;

        #endregion

        #region Constructors

        public CaseContract()
        {
            this.id = Guid.NewGuid();
            this.clientPortalId = Guid.NewGuid();
            this.name = string.Empty;
        }

        #endregion

        #region Properties

        [DataMember]
        public Guid Id
        {
            get { return this.id; }
            set 
            {
                if (!object.ReferenceEquals(this.id, value))
                {
                    this.id = value;
                    PropertyChangedHelper.RaisePropertyChanged(this.PropertyChanged, 
                        this, "Id");
                }
            }
        }

        [DataMember]
        public Guid ClientPortalId
        {
            get { return this.clientPortalId; }
            set 
            {
                if (!object.ReferenceEquals(this.clientPortalId, value))
                {
                    this.clientPortalId = value;
                    PropertyChangedHelper.RaisePropertyChanged(this.PropertyChanged,
                        this, "ClientPortalId");
                }
            }
        }

        [DataMember]
        public string Name
        {
            get { return this.name; }
            set 
            {
                if (!object.ReferenceEquals(this.name, value))
                {
                    this.name = value;
                    PropertyChangedHelper.RaisePropertyChanged(this.PropertyChanged,
                        this, "Name");
                }
            }
        }

        [DataMember]
        public string Url
        {
            get { return this.url; }
            set
            {
                if (!object.ReferenceEquals(this.url, value))
                {
                    this.url = value;
                    PropertyChangedHelper.RaisePropertyChanged(this.PropertyChanged,
                        this, "Url");
                }
            }
        }

        #endregion

        #region Equality Tests

        /// <summary>
        /// Determines whether the specified entity is equal to the 
        /// current instance.
        /// </summary>
        /// <param name="entity">An <see cref="System.Object"/> that 
        /// will be compared to the current instance.</param>
        /// <returns>True if the passed in entity is equal to the 
        /// current instance.</returns>
        public override bool Equals(object contract)
        {
            return contract != null
                && contract is CaseContract
                && this == (CaseContract)contract;
        }

        /// <summary>
        /// Operator overload for determining equality.
        /// </summary>
        /// <param name="base1">The first instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <param name="base2">The second instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <returns>True if equal.</returns>
        public static bool operator ==(CaseContract base1,
            CaseContract base2)
        {
            // check for both null (cast to object or recursive loop)
            if ((object)base1 == null && (object)base2 == null)
            {
                return true;
            }

            // check for either of them == to null
            if ((object)base1 == null || (object)base2 == null)
            {
                return false;
            }

            // Make sure the Key values match
            if (base1.Id != base2.Id ||
                base1.ClientPortalId != base2.ClientPortalId ||
                base1.Name != base2.Name)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Operator overload for determining inequality.
        /// </summary>
        /// <param name="base1">The first instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <param name="base2">The second instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <returns>True if not equal.</returns>
        public static bool operator !=(CaseContract base1,
            CaseContract base2)
        {
            return (!(base1 == base2));
        }

        /// <summary>
        /// Serves as a hash function for this type.
        /// </summary>
        /// <returns>A hash code for the current Key 
        /// property.</returns>
        public override int GetHashCode()
        {
            return this.id.GetHashCode() ^
                this.clientPortalId.GetHashCode() ^
                this.name.GetHashCode();
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region ToString

        public override string ToString()
        {
            return this.Name;
        }

        #endregion
    }
}
